<!-- 确认订单 -->
<template>
  <div style="background-color: #fff; min-height: calc(100vh)">
    <div style="margin: 15px">
      <div class="banxing">
        <div class="imgs">
          <img :src="tghklist.cover" alt />
        </div>
        <div>
          <div class="banxing_title">{{ tghklist.name }}</div>
          <div class="banxing_button">
            <span>
              <i style="font-size: 12px">¥</i>
              <i>{{ price }}</i>
            </span>
          </div>
        </div>
      </div>
    </div>

    <div v-if="tghklist.is_entity == 1">
      <div class="tishi">
        <i>*</i>
        {{ tghklist.entity_info }}
      </div>
      <div class="headline clearfix">
        <span class="h_left">添加收货信息</span>
        <span class="h_right" @click="toSelectAddress">
          去修改
          <van-icon name="arrow" />
        </span>
      </div>

      <div class="m15" v-if="addressList.phone">
        <div class="name_phone">
          <van-icon :name="require('@/assets/images/dizhi.png')" />
          <span>{{ addressList.name }}</span>
          <span>{{ addressList.phone }}</span>
        </div>
        <div class="dizhi">
          {{ addressList.province_name }}
          <span>{{ addressList.city_name }}</span>
          <span>{{ addressList.town_name }}</span>
          <span>{{ addressList.addr }}</span>
        </div>
      </div>
    </div>

    <van-submit-bar
      label="应支付："
      :disabled="isDisabledSubmitBtn"
      :decimal-length="2"
      :price="parseFloat(price) * 100"
      button-text="立即支付"
      @submit="onSubmit"
      button-color="#009697"
    />

    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="block">
          <img :src="require('@/assets/images/chenggongicon.png')" alt />
          <h3>支付成功就可以开始学习了！</h3>
          <van-row justify="space-around">
            <van-col span="12">
              <van-button round class="button1" @click="toHome">返回首页</van-button>
            </van-col>
            <van-col span="12">
              <van-button round class="button2" @click="toStudy">立即学习</van-button>
            </van-col>
          </van-row>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { Toast } from 'vant'
// import { getCode } from '@/utils'
import { makeOrders } from '@/api/home/confirm-order'
import { getAddress } from '@/api/mine/personal'
export default {
  data() {
    return {
      isDisabledSubmitBtn: false,
      // tghklist: {},
      price: '0.00',
      addressList: {},
      weChatParameter: {},
      // 支付成功弹框
      show: false
    }
  },

  computed: {
    tghklist() {
      console.log(this.$store.getters.goodsList)
      return this.$store.getters.goodsList
    },
    loginList() {
      return this.$store.getters.loginList
    }
  },

  mounted() {
    if (this.tghklist.sku_id > 0) {
      const skuP = this.tghklist.sku
      this.price = skuP.filter(res => res.id === this.tghklist.sku_id)[0].price
    } else {
      this.price = this.tghklist.price
    }
    this.fetchList()
    // if (this.loginList && (this.loginList.wxs_openid === 0)) {
    //   getCode()
    // }
  },

  methods: {
    // 获取地址
    fetchList() {
      if (this.$route.query.row) {
        this.addressList = JSON.parse(decodeURIComponent(this.$route.query.row))
      } else {
        const data = {
          is_default: 1
        }
        getAddress(data).then(res => {
          this.addressList = res.data[0] || {}
        })
      }
    },
    toSelectAddress() {
      this.$router.push({ name: 'SelectAddress', params: { id: this.addressList.id } })
    },
    toHome() {
      this.$router.push({ name: 'Home' })
    },
    toStudy() {
      this.$router.push({ path: this.tghklist.path, query: this.tghklist.query })
    },
    // 提交订单
    onSubmit() {
      if (this.tghklist.is_entity == 1 && !this.addressList.id) {
        return Toast('请选择地址')
      }
      const data = {
        goods_id: this.tghklist.goods_id,
        sku_id: this.tghklist.sku_id,
        client: 'android',
        share_token: this.tghklist.share_token,
        address_id: this.tghklist.is_entity == 1 ? this.addressList.id : null
      }
      this.isDisabledSubmitBtn = true
      makeOrders(data)
        .then(res => {
          this.weChatParameter = res.data.orderinfo
          this.weixinPay()
        })
        .catch(error => {
          this.isDisabledSubmitBtn = false
          Toast(error.msg)
        })
    },

    // h5方法调取微信支付
    weixinPay(params) {
      var that = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', that.onBridgeReady(params), false)
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', that.onBridgeReady(params))
          document.attachEvent('onWeixinJSBridgeReady', that.onBridgeReady(params))
        }
      } else {
        that.onBridgeReady()
      }
    },
    // 微信内置浏览器类，weChatParameter对象中的参数是3.步骤代码中从后端获取的数据
    onBridgeReady() {
      var that = this
      // var timestamp = Math.round(that.weChatParameter.timeStamp).toString()
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          debug: true,
          appId: that.weChatParameter.appId, // 公众号名称，由商户传入
          timeStamp: that.weChatParameter.timeStamp, // 时间戳，自1970年以来的秒数
          nonceStr: that.weChatParameter.nonceStr, // 随机串
          package: that.weChatParameter.package,
          signType: that.weChatParameter.signType, // 微信签名方式：
          paySign: that.weChatParameter.paySign, // 微信签名
          jsApiList: ['chooseWXPay']
        },
        function (res) {
          // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            // 支付成功后的操作
            that.$toast({
              message: '支付成功',
              duration: 2000
            })
            that.show = true
            that.isDisabledSubmitBtn = true
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            // 取消支付的操作
            that.$toast({
              message: '取消支付',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          } else {
            // 支付失败的操作
            that.$toast({
              message: '支付失败',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          }
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.banxing {
  display: flex;
  .imgs {
    margin-right: 9px;
    img {
      width: 150px;
      height: 90px;
      border-radius: 5px;
      overflow: hidden;
    }
  }
}
.banxing_title {
  height: 40px;
  font-size: 15px;
  font-weight: bold;
  color: #333333;
  line-height: 21px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.banxing_button {
  margin-top: 10px;
  font-size: 14px;
  font-weight: 500;
  color: #009697;
  span {
    display: inline-block;
    font-size: 16px;
    font-weight: 800;
    color: #009697;
    line-height: 30px;
  }
}
.banxing_zhixun {
  font-size: 12px;
  font-weight: 500;
  color: #999999;
  line-height: 30px;
  span {
    font-size: 12px;
    font-weight: 500;
    text-decoration: line-through;
    color: #999999;
    line-height: 30px;
  }
}

.tishi {
  box-sizing: border-box;
  height: 63px;
  padding: 15px;
  background: #f6f6f6;
  font-size: 13px;
  font-weight: 400;
  color: #6c6c6c;
  line-height: 20px;
  i {
    color: #fc7200;
  }
}

.headline {
  margin: 20px 15px 18px;
  .h_left {
    float: left;
    font-size: 14px;
    font-weight: bold;
    color: #333333;
  }
  .h_right {
    float: right;
    font-size: 11px;
    font-weight: bold;
    color: #009697;
    .van-icon {
      vertical-align: middle;
      margin-bottom: 2px;
    }
  }
}

.name_phone {
  font-size: 14px;
  font-weight: bold;
  color: #333333;
  .van-icon {
    vertical-align: text-top;
  }
  span {
    margin-left: 10px;
  }
}
.dizhi {
  margin: 20px 0 0 25px;
  font-size: 14px;
  font-weight: bold;
  color: #666666;
  line-height: 20px;
  span {
    margin-left: 10px;
  }
}

::v-deep .van-submit-bar {
  height: 70px;
  background: #ffffff;
  box-shadow: 0px -1px 4px 0px rgba(66, 66, 66, 0.19);
  .van-submit-bar__bar {
    height: 70px;
  }
  .van-submit-bar__price {
    color: #009697;
  }
  .van-button {
    width: 130px;
    height: 44px;
    line-height: 44px;
    background: #009697;
    border-radius: 22px;
    font-size: 16px;
  }
}

// 支付成功弹框
::v-deep .van-overlay {
  z-index: 999;
}
.wrapper {
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
  height: 100%;
  .block {
    width: 255px;
    height: 258px;
    background: #f9f9f9;
    border-radius: 5px;
  }
  img {
    width: 133px;
    height: 110px;
    margin-top: 21px;
  }
  h3 {
    font-weight: 500;
  }
  .van-button {
    margin-top: 20px;
    width: 90px;
    height: 38px;
    padding: 0;
    font-size: 15px;
    font-weight: 500;
    color: #333333;
  }
  .button1 {
    background: #f3f3f3;
    color: #333333;
  }
  .button2 {
    background: #009697;
    color: #ffffff;
  }
}
</style>
